﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Reflection;
using System.IO;
using Superstore.Store.PushMessage;
using Superstore.Store.Models;

namespace Superstore.Store.Controllers
{
    public class NoticeController : Controller
    {
        // GET: Notice
        public ActionResult Query(string userId, string tick)
        {
            UserMessage userMsg = MessageMgr.Getinstance().GetUserMessage(UserIdType.NormalId, userId, tick);
            NoticeInfo noticeInfo = new NoticeInfo();
            if (true == userMsg.IsEmpty())
            {
                noticeInfo.result = -1;
            }
            else
            {
                noticeInfo.messages = userMsg.GetMessages();
                noticeInfo.result = 0;
            }

            string result = JsonConvert.SerializeObject(noticeInfo, Formatting.Indented);
            result = result.Replace("\\\"", "\"");
            return Content(result, "application/json");
        }

        class PushItem
        {
            public UserIdType type { get; set; }
            public string userId { get; set; }
            public string msg { get; set; }
        }

        private static string NotifySessionKey = "notify_session";
        private static string UserIdKey = "user_id";
        private static string OpenIdKey = "open_id";
        private PushItem Parse(string jsonString)
        {
            JObject json = JObject.Parse(jsonString);
            JToken session = json.GetValue(NotifySessionKey);
            PushItem pushItem = null;
            if (null != session && session.HasValues)
            {
                json.Remove(NotifySessionKey);
                pushItem = new PushItem();
                pushItem.msg = jsonString;

                string userId = session[UserIdKey].ToString();
                string openId = session[OpenIdKey].ToString();
                if (!string.IsNullOrEmpty(userId))
                {
                    pushItem.type = UserIdType.NormalId;
                    pushItem.userId = userId;
                }
                else if (!string.IsNullOrEmpty(openId))
                {
                    pushItem.type = UserIdType.WeixinId;
                    pushItem.userId = openId;
                }
            }
            return pushItem;
        }

        private ActionResult HandlePushRequest(string protocalName)
        {
            var resolveRequest = HttpContext.Request;
            resolveRequest.InputStream.Seek(0, SeekOrigin.Begin);
            string jsonString = new StreamReader(resolveRequest.InputStream).ReadToEnd();

            string jsonMsg;
            PushItem item = Parse(jsonString);
            if (null != item)
            {
                Message msg = new Message(protocalName, item.msg);
                MessageMgr.Getinstance().AddUserMessage(item.type, item.userId, msg);
                jsonMsg = "{\"result\":0}";
            }
            else
            {
                jsonMsg = string.Format("{\"result\": -1,\r\n\"msg\": {0}}", "notify_session is null");
            }
            return Content(jsonMsg, "application/json");
        }

        [HttpPost]
        public ActionResult kick_out_notice()
        {
            string protocalName = MethodBase.GetCurrentMethod().Name;
            return HandlePushRequest(protocalName);
        }

        [HttpPost]
        public ActionResult order_changed_notify()
        {
            string protocalName = MethodBase.GetCurrentMethod().Name;
            return HandlePushRequest(protocalName);
        }
    }
}